package com.usian.user.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.usian.common.dto.CourseDto;
import com.usian.common.vo.ResponseResult;
import com.usian.user.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/course")
public class CourseController {

    @Autowired
    private CourseService courseService;


    @PostMapping("/getHotCourseList")
    public ResponseResult getHotCourseList(@RequestBody CourseDto courseDto) {
        return courseService.getHotCourseList(courseDto);
    }

    @PostMapping("/getNewestCourseList")
    public ResponseResult getNewestCourseList(@RequestBody CourseDto courseDto) {
        return courseService.getNewestCourseList(courseDto);
    }

    @PostMapping("/getCourseList")
    public ResponseResult getCourseList(@RequestBody CourseDto courseDto) {
        return courseService.getCourseList(courseDto);
    }

    @GetMapping("/getLab")
    public ResponseResult getLab() {
        return courseService.getLab();
    }

    @GetMapping("/getCourseById")
    public ResponseResult getCourseById(Integer courseId) {
        return courseService.getCourseById(courseId);
    }

    @GetMapping("getProgressData")
    public ResponseResult getProgressData(HttpServletRequest request, Integer courseId) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("userId");
        return courseService.getProgressData(courseId, userId);
    }

    @GetMapping("/getCatalogue")
    public ResponseResult getCatalogue(Integer courseId) {
        return courseService.getCatalogue(courseId);
    }

    @GetMapping("/setProgress")
    public ResponseResult setProgress(Integer cataId, Integer courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        JWT jwt = JWTUtil.parseToken(token);
        Integer userId = (Integer) jwt.getPayload("userId");
        return courseService.setProgress(cataId, userId, courseId);
    }
}
